<?php
namespace app\index\controller;
use think\Controller;
class Common extends Controller{
	
	public function _initialize()
	{
		session_start();
		$lang = "en";
		
		
		if(isset($_SESSION['lang']))
		{
			$lang = $_SESSION['lang'];
		}
		
		$this->assign("skey",time());
		$this->assign("lang",$lang);
	}
	
    /**
     * Reduce a string by the middle, keeps whole words together
     *
     * @param string $string
     * @param int $max (default 50)
     * @param string $replacement (default [...])
     * @return string
     * @author david at ethinkn dot com
     * @author loic at xhtml dot ne
     * @author arne dot hartherz at gmx dot net
     */
    function strSubx($string,$max=50,$rep='[...]'){
      $strlen = strlen($string);
      if ($strlen < $max)
        return $string;
      $lengthtokeep = $max - strlen($rep);
      $start = 0;
      $end = 0;
      if (($lengthtokeep % 2) == 0) {
        $start = $lengthtokeep / 2;
        $end = $start;
      } else {
        $start = intval($lengthtokeep / 2);
        $end = $start + 1;
      }
      $i = $start;
      $tmp_string = $string;
      while ($i < $strlen) {
        if (isset($tmp_string[$i]) and $tmp_string[$i] == ' ') {
          $tmp_string = substr($tmp_string, 0, $i) . $rep;
          $return = $tmp_string;
        }
        $i++;
      }
      $i = $end;
      $tmp_string = strrev ($string);
      while ($i < $strlen) {
        if (isset($tmp_string[$i]) and $tmp_string[$i] == ' ') {
          $tmp_string = substr($tmp_string, 0, $i);
          $return .= strrev ($tmp_string);
        }
        $i++;
      }
      return substr($string, 0, $start).$rep.substr($string, - $end);
    }
	
	
	//计算时间差
	function time_ago($posttime){
		//当前时间的时间戳
		$nowtimes = strtotime(date('Y-m-d H:i:s'),time());
		//之前时间参数的时间戳
		//$posttimes = strtotime($posttime);
		//相差时间戳
		$counttime = $nowtimes - $posttime;
		//进行时间转换
		if($counttime<=60){
			return $counttime."s ago";
		}else if($counttime>60 && $counttime<3600){
			return intval(($counttime/60)).'min ago';
		}else if($counttime>=3600 && $counttime<3600*24){
			return intval(($counttime/3600)).'hou ago';
		}else if($counttime>=3600*24){
			return intval(($counttime/(3600*24))).'day ago';
		}
	}
	
	//CURL
	function curl_request($url,$post=''){
			$curl = curl_init();
			curl_setopt($curl, CURLOPT_URL, $url);
			if($post) {
				curl_setopt($curl, CURLOPT_POST, 1);
				curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
			}
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
			$data = curl_exec($curl);
			if (curl_errno($curl)) {
				return curl_error($curl);
			}
			curl_close($curl);
			return $data;
	}
	
	/** Json数据格式化
	* @param  Mixed  $data   数据
	* @param  String $indent 缩进字符，默认4个空格
	* @return JSON
	*/
	function jsonFormat($data, $indent=null){
		// 对数组中每个元素递归进行urlencode操作，保护中文字符
		array_walk_recursive($data, [$this,'jsonFormatProtect']);
		// json encode
		$data = json_encode($data);
		// 将urlencode的内容进行urldecode
		$data = urldecode($data);
		// 缩进处理
		$ret = '';
		$pos = 0;
		$length = strlen($data);
		$indent = isset($indent)? $indent : '    ';
		$newline = "\n";
		$prevchar = '';
		$outofquotes = true;
		for($i=0; $i<=$length; $i++){
			$char = substr($data, $i, 1);
			if($char=='"' && $prevchar!='\\'){
				$outofquotes = !$outofquotes;
			}elseif(($char=='}' || $char==']') && $outofquotes){
				$ret .= $newline;
				$pos --;
				for($j=0; $j<$pos; $j++){
					$ret .= $indent;
				}
			}
			$ret .= $char;
			if(($char==',' || $char=='{' || $char=='[') && $outofquotes){
				$ret .= $newline;
				if($char=='{' || $char=='['){
					$pos ++;
				}
				for($j=0; $j<$pos; $j++){
					$ret .= $indent;
				}
			}
			$prevchar = $char;
		}
		return $ret;
	}
	/** 将数组元素进行urlencode
	* @param String $val
	*/
	function jsonFormatProtect(&$val){
		if($val!==true && $val!==false && $val!==null){
			$val = urlencode($val);
		}
	}
	
	//12位随机数
    function rnd_gen( $length = 12 ) {
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $rstr = '';
        for ( $i = 0; $i < $length; $i++ ) 
        {
            $rstr .= $chars[ mt_rand(0, strlen($chars) - 1) ];
        }
        return $rstr;
    }
	
	//处理算力
	public function chk_power($num,$i=0)
	{
		
		//$cnum = $num/1024;
		$num = (int)$num;
		$cnum = round($num / 1024,4);
		
		if(strlen((string)$cnum)>5)
		{
			$i++;
			return $this->chk_power($cnum,$i);
		}
		
		switch($i)
		{
			case 0:$d="KB";break;
			case 1:$d="MB";break;
			case 2:$d="GB";break;
			default:$d="";break;
		}
		
		return $cnum.$d;
		
	}
	
	
	//处理科学计数法
	public function sctonum($num, $double = 12){
		if(false !== stripos($num, "e")){
			$a = explode("e",strtolower($num));
			return bcmul($a[0], bcpow(10, $a[1], $double), $double);
		}
		return $num;
	}
	
	//处理数字
	function chk_num($num,$type=1)
	{
		if(!$num)
		{
			return 0;
		}
		
		
		//处理余额 18个0
		if($type==1)
		{
			$num = bcdiv($num,1000000000000000000);
		}
		
		if($type==2)
		{
			$num = $num/100000;
		}
		
		if($type==3)
		{
			$num = bcmul($num,1000000000000000000);
		}
		
		if($type==4)
		{
			$num = bcmul($num,100000);
		}
		
		if($type==5)
		{
			$num = bcdiv($num,1000000000000000000,18);
		}
		
		return $num;
	}
	
	//指定数组以$keys键值排序
	function u_array_sort($array,$keys,$type='asc'){
		//$array为要排序的数组,$keys为要用来排序的键名,$type默认为升序排序
		$keysvalue = $new_array = array();
		foreach ($array as $k=>$v){
			$keysvalue[$k] = $v[$keys];
		}
		if($type == 'asc'){
			asort($keysvalue);
		}else{
			arsort($keysvalue);
		}
		reset($keysvalue);
		foreach ($keysvalue as $k=>$v){
			$new_array[$k] = $array[$k];
		}
		return $new_array;
	}
	
	//替换字符串
	function func_substr_replace($str, $replacement = '*', $start = 1, $length = 3){
		$len = mb_strlen($str,'utf-8');
		if ($len > intval($start+$length)) {
			$str1 = mb_substr($str,0,$start,'utf-8');
			$str2 = mb_substr($str,intval($start+$length),NULL,'utf-8');
		} else {
			$str1 = mb_substr($str,0,1,'utf-8');
			$str2 = mb_substr($str,$len-1,1,'utf-8');    
			$length = $len - 2;        
		}
		$new_str = $str1;
		for ($i = 0; $i < $length; $i++) { 
			$new_str .= $replacement;
		}
		$new_str .= $str2;
		return $new_str;
	}
}